import Vue from 'vue'
//npm i vue-router@3.5.3
import VueRouter from 'vue-router'
//引入路由
import routes from './routes'
//引入仓库
import store from '@/store/index'

Vue.use(VueRouter)

console.log(store)
let router = new VueRouter({
  mode:'history',
  routes
})
router.beforeEach((to,form,next)=>{
  store.commit('getToken')
  const token = store.state.user.token
  if (!token && to.path!=='/login'){
    next({name:'Login'})
  }else if(token && to.path==='/login'){
    next('/')
  }else{
    next()
  }
})
export default router
